$(document).ready(function(){
	
	$("#modifyUser").val(username);
	
	//获取参数typeID
	var srclast = tl_config.last_url;	
	var ii = srclast.indexOf('?');
	var jj =srclast.indexOf('&');
	
	if(ii!=-1){		
		var typeId = srclast.substring(ii+4,jj);
		var typeName=srclast.substring(jj+10);	
		$("#typeId").val(typeId);
		$("#typeName").val(typeName);
		$("p#typeNamedesc").text(typeName);
	}
	
	//获取当前字典Id
	var src = tl_config.his_url;
	var i = src.indexOf('?');
	if(i!=-1){
		var id = src.substring(i+4);
        $.ajax({
        	url:server_url+'rest/dictionary/selectDictionaryById.do?ran='+Math.random()+ '&token=' + token,
        	type:'get',
        	data:{id:id},
        	success:function(data){
        		if(data.retCode==='0000'){        			
        			$("#id").val(data.data.id);
        			$("#dicName").val(data.data.dicName);        			
        			$("#dicValue").val(data.data.dicValue);
        			$("#dicDesc").val(data.data.dicDesc);
        		}
        	}
        });
	}	
	
	//表单验证
	var validate = $("#fromEditDictionary").validate({		   
		rules:{					
			dicName:{
				  required:true,
			      remote: { 
	                   url: server_url+'/rest/dictionary/checkDicNameEdit.do?timestamp='+Math.random()+ '&token=' + token,
	                   type: "post",
	                   dataType: "json", 
	                   data: {	id: function() { return $("#id").val();},	                	        
	                	        typeId: function() { return $("#typeId").val();},
	                	        dicName: function() { return $("#dicName").val();}
	                         } 
	               },
	               stringMaxLength:30
			},
			dicValue:{
				  required:true,
			      remote: { 
	                   url: server_url+'/rest/dictionary/checkDicValueEdit.do?timestamp='+Math.random()+ '&token=' + token,
	                   type: "post",
	                   dataType: "json", 
	                   data: {	
	                	       id: function() { return $("#id").val();},
	                	       typeId: function() { return $("#typeId").val();},
	                	       dicValue: function() { return $("#dicValue").val();}
	                         } 
	               },
	               stringMaxLength:30
			}
		},
		messages:{				
			     dicName:{
			           required:"该值不能为空！",
                       remote:"字典名已存在，请重新输入!",
                       stringMaxLength:"最多能输入30个字符！"
                   },
                   dicValue:{
				       required:"该值不能为空！",
	                   remote:"字典值已存在，请重新输入!",
	                   stringMaxLength:"最多能输入30个字符！"
	                 }
		},
		submitHandler:function(){
			sub();
		},
		errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo ( element.parent() );
			else if ( element.is(":checkbox") )
				error.appendTo ( element.parent() );
			else if ( element.is("input[name=captcha]") )
				error.appendTo ( element.parent() );
			else
				error.insertAfter(element);
		},
		validClass : 'rightValidate',
	    success: function(label) {
		   label.html("&nbsp;").text(" ") ;
	    }
 });	

	
});


//提交
function sub(){
	if($("#fromEditDictionary").valid()){
		$.ajax({
			url:server_url+'/rest/dictionary/dictionaryEdit.do?ran='+Math.random()+ '&token=' + token,
			type:'post',
			data:$("#fromEditDictionary").serialize(),
			success:function(data){
				easyDialog.open({container:{header:'温馨提示',content:data.retMsg},autoClose : 2000});
				back();
			}
		});
	}
}


function back(){
//	$('.g_rightMidden').html("");
//	$(".g_rightMidden").load("dictionaryList.do?ran="+Math.random());
	
	var url="dictionaryList.do?ran="+Math.random();
	tl_config.loadToWorkArea(url);
}

function submitCommit(){
	$("#fromEditDictionary").submit();
}
